﻿
Imports System.Data
Imports VSModel
Imports Cls_Common
Imports SecurityService

Partial Class Control_MaSoThue_ContentEditor
    Inherits System.Web.UI.UserControl
    Private Shared ReadOnly log As log4net.ILog = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType)
#Region "Sub and Function "
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            setPermission()
            If Not Request.QueryString("Certifiedid") Is Nothing Then
                hidID.Value = Request.QueryString("Certifiedid")
                'Load list action status
                LoadListAction(hidID.Value)
                setPermission()
                'btnDelete.Visible = HasPermission(Function_Name.CertifiedHousehold, Session("RoleID"), 0, Audit_Type.Delete)
            End If
            'active tab tuong ung voi Page
            'InVisble button ko can thiet
            If Request.PhysicalPath.Contains("List.aspx") Then
                lbtList.CssClass = "current_manage"
                lbtList.Text = "<span class='current_manage'>Quản lý</span>"
                lbtUpdateMST.Visible = False
                lbtView.Visible = False
                btnDelete.Visible = False
                'Chi ap dung cho du lieu xuat ban
                tabs_state.Visible = False
                cboAction.Visible = False
                'lbtInPhieu.Visible = False
                'Permission
            ElseIf Request.PhysicalPath.Contains("Create.aspx") Or Request.PhysicalPath.Contains("CapDoi.aspx") Or Request.PhysicalPath.Contains("CapMoi.aspx") Then
                lbtAdd.CssClass = "current_manage"
                lbtAdd.Text = "<span class='current_manage'>Thêm mới</span>"
                lbtUpdateMST.Visible = False
                lbtView.Visible = False
                btnDelete.Visible = False
                'Chi ap dung cho du lieu xuat ban
                tabs_state.Visible = False
                cboAction.Visible = False
                'lbtInPhieu.Visible = False

            ElseIf Request.PhysicalPath.Contains("Detail.aspx") Then
                lbtView.CssClass = "current_manage"
                lbtView.Text = "<span class='current_manage'>Xem</span>"

                lbtView.Visible = True
                btnDelete.Visible = False


            ElseIf Request.PhysicalPath.Contains("ImportFromExcel.aspx") Then
                lbtImport.CssClass = "current_manage"
                lbtImport.Text = "<span class='current_manage'>Import excel file</span>"

                lbtView.Visible = False
                btnDelete.Visible = False
                'Chi ap dung cho du lieu xuat ban
                tabs_state.Visible = False
                cboAction.Visible = False

            ElseIf Request.PhysicalPath.Contains("Update.aspx") Then

                lbtView.Visible = False
                btnDelete.Visible = False
                'Chi ap dung cho du lieu xuat ban
                tabs_state.Visible = False
                cboAction.Visible = False
                lbtUpdateMST.CssClass = "current_manage"
                lbtUpdateMST.Text = "<span class='current_manage'>Cập nhật MST</span>"
            ElseIf Request.PhysicalPath.Contains("Action.aspx") Then


                btnDelete.Visible = False


            End If



        End If
    End Sub
    Protected Sub setPermission()
        'Se bat lai khi dua chuc nang phan quyen
        lbtAdd.Visible = HasPermission(Function_Name.CertifiedHousehold, Session("RoleID"), hidStatusId.Value, Audit_Type.Create)
        lbtView.Visible = HasPermission(Function_Name.CertifiedHousehold, Session("RoleID"), hidStatusId.Value, Audit_Type.ViewContent)
        lbtUpdateMST.Visible = HasPermission(Function_Name.CertifiedHousehold, Session("RoleID"), hidStatusId.Value, Audit_Type.Edit)
        btnDelete.Visible = HasPermission(Function_Name.CertifiedHousehold, Session("RoleID"), hidStatusId.Value, Audit_Type.Delete)
        'lbtInPhieu.Visible = HasPermission(Function_Name.CertifiedHousehold, Session("RoleID"), 1, Audit_Type.Create)
    End Sub
    Protected Sub lbtList_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lbtList.Click
        Response.Redirect("List.aspx")
    End Sub
    Protected Sub lbtView_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lbtView.Click
        Response.Redirect("Detail.aspx?CertifiedID=" & hidID.Value & "")
    End Sub
    Protected Sub lbtImport_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lbtImport.Click
        Response.Redirect("ImportFromExcel.aspx", True)
    End Sub

    'Chi dung cho du lieu xuat ban
    'Load list action status of current status
    Sub LoadListAction(ByVal iCertifiedHouseholdId As Integer)
        Using Data As New VSEntities
            ' Get Current StatusId of object
            Dim cst = (From q In Data.CertifiedHouseholds Where q.CertifiedID = iCertifiedHouseholdId Select q).SingleOrDefault
            hidStatusId.Value = cst.StatusId
            Dim iCurrentStatusId As Byte = cst.StatusId
            'Load action tuong ung voi current statusid
            Dim st = (From q In Data.StatusActions Where q.CurrentStatusId = iCurrentStatusId And q.FunctionId = Function_Name.CertifiedHousehold Select q Order By
                     q.ActionId, q.Action.Title, q.Action.AuditNumber).ToList()
            For Each i In st
                If HasPermission(Function_Name.CertifiedHousehold, Session("RoleID"), i.CurrentStatusId, i.Action.AuditNumber) Then
                    Dim _item As New ListItem("---" & i.Action.Title, i.ActionId)
                    cboAction.Items.Insert(0, _item)
                End If
            Next
            Dim _status = (From q In Data.Status Where q.StatusId = iCurrentStatusId And q.FunctionId = Function_Name.CertifiedHousehold).FirstOrDefault
            If Not _status Is Nothing Then
                Dim lstItem As New ListItem(_status.Title, "")
                cboAction.Items.Insert(0, lstItem)
                cboAction.SelectedIndex = -1
            End If
        End Using
    End Sub
    'Chi dung cho du lieu xuat ban
    Protected Sub cboAction_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cboAction.SelectedIndexChanged
        'Nếu trạng thái đang chọn khác với trạng thái ban đầu thì mới đi chuyển trạng thái
        If (cboAction.SelectedValue <> cboAction.Items(0).Value) Then
            Response.Redirect("Action.aspx?Certifiedid=" & hidID.Value & "&ActionId=" & cboAction.SelectedValue)
        End If
    End Sub
#End Region

    
    Protected Sub lbtUpdateMST_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lbtUpdateMST.Click
        Response.Redirect("Update.aspx?CertifiedID=" & hidID.Value & "")
    End Sub
End Class
